#include <commandSetReadSN.h>
#include <deviceParameter.h>
#include <commandList.h>

#if COMMAND_MASTER_CALL_DEFINE
int masterCallbackSetReadSN(void *inputstring, uint32_t *inputsize, void *userdata)
{
    struct protocolPackageCommonInform *tmppack = (struct protocolPackageCommonInform *)inputstring;
    struct commandCommonStruct *tmpcommand = (struct commandCommonStruct *)tmppack->pv2.v2Data;
    struct deviceStructSetReadSN *tmpinform = (struct deviceStructSetReadSN *)tmpcommand->commandData;
    // struct protocolProcessorInform *tmpprocessor = (struct protocolProcessorInform *)userdata;
    if (!inputstring || !inputsize || !userdata)
    {
        logerror("");
        return -1;
    }
    logdebug("device[%d]'s version: %s, sn: %.*s, type=%d", tmppack->pv2.v2SourceAddr, tmpinform->version,
             (int)sizeof(tmpinform->serialnumber), tmpinform->serialnumber, tmpinform->deviceType);
    return 0;
}
int masterWaitResponseSetReadSN(struct protocolProcessorInform *processer, struct masterStructSetReadSN *indata, uint16_t insize, uint8_t devaddr)
{
    int tmpflag = 0;
    if (!processer || !devaddr)
    {
        logerror("");
        return -1;
    }
    uint8_t tmpPackBuffer[PROTOCOL_PACK_MAX_SIZE];
    struct protocolPackageCommonInform *tmppack = (struct protocolPackageCommonInform *)tmpPackBuffer;
    struct commandCommonStruct *tmpcommand = (struct commandCommonStruct *)tmppack->pv2.v2Data;
    if ((PROTOCOL_V2_COMMON_HEAD_SIZE + PROTOCOL_V2_DATA_SIZE(insize)) > sizeof(tmpPackBuffer))
    {
        logerror("data big:%d", (int)PROTOCOL_V2_DATA_SIZE(insize));
        return 0;
    }
    processer->commandExcuterAddr = devaddr;
    processer->commandExcuter.command = commandIdSetReadSN;
    processer->commandExcuter.serialNumber = generateCommandSerial();
    processer->commandExcuter.responseFlag = commandExcuteResultUnkown;
    memcpy(tmpcommand, &processer->commandExcuter, PROTOCOL_COMMAND_COMMON_HEAD_SIZE);
    memcpy(tmpcommand->commandData, indata, insize);
    tmppack->pv2.v2DataSize = PROTOCOL_V2_DATA_SIZE(insize);
    tmpflag = processerPackageProtocolData(processer, tmpPackBuffer, sizeof(tmpPackBuffer));
    if (tmpflag <= 0)
    {
        logerror("");
        return -1;
    }
    masterProcesserSetReTryCount(processer, 2);
    masterProcesserSetTimeout(processer, 400);
    tmpflag = masterProcesserSendAndWaitResponse(processer, tmpPackBuffer, tmpflag);
    return tmpflag;
}
#endif

#if COMMAND_DEVICE_CALL_DEFINE
struct deviceParameterOperate *deviceParamOperatePcCheckInstance();
int deviceCallbackSetReadSN(void *inputstring, uint32_t *inputsize, void *userdata)
{
    struct protocolPackageCommonInform *tmppack = (struct protocolPackageCommonInform *)inputstring;
    // struct protocolVersion2 *tmpv2 = &tmppack->pv2;
    struct commandCommonStruct *tmpcommand = (struct commandCommonStruct *)tmppack->pv2.v2Data;
    // struct protocolProcessorInform *tmpprocessor = (struct protocolProcessorInform *)userdata;
    struct masterStructSetReadSN *tmpCmdData = (struct masterStructSetReadSN *)tmpcommand->commandData;
    struct deviceStructSetReadSN *tmpResponseData = (struct deviceStructSetReadSN *)tmpcommand->commandData;
    int tmpDataSize;
    if (!inputstring || !inputsize || !userdata)
    {
        logerror("");
        return -1;
    }
    tmpDataSize = tmppack->pv2.v2DataSize - PROTOCOL_COMMAND_COMMON_HEAD_SIZE;
    if (tmpDataSize != sizeof(struct masterStructSetReadSN))
    {
        logerror("input data error :%d, %d", tmpDataSize, (int)sizeof(struct masterStructSetReadSN));
        return -1;
    }
    if (sizeof(struct deviceStructSetReadSN) > PROTOCOL_V2_COMMAND_DATA_SIZE)
    {
        logerror("size error:%d>%d", (int)sizeof(struct deviceStructSetReadSN), (int)PROTOCOL_V2_COMMAND_DATA_SIZE);
        return -1;
    }

    if (devNorParmInst()->deviceSerialNumber[0] == 0 && devNorParmInst()->deviceSerialNumber[1] == 0)
    {
        memcpy(devNorParmInst()->deviceSerialNumber, tmpCmdData->setSn, sizeof(tmpCmdData->setSn));
        logdebug("gen ok:%.*s", DEVICE_SERIAL_NUMBER_SIZE, devNorParmInst()->deviceSerialNumber);
        deviceParamWriteToHard(deviceParamOperatePcCheckInstance());
    }

    inputsize[0] = sizeof(struct deviceStructSetReadSN);
    memcpypro(tmpResponseData->version, sizeof(tmpResponseData->version),
              "DOOR-LOCK-STD.V1.0.202401051725", sizeof("DOOR-LOCK-STD.V1.0.202401051725"));
    memcpypro(tmpResponseData->serialnumber, sizeof(tmpResponseData->serialnumber),
              devNorParmInst()->deviceSerialNumber, sizeof(devNorParmInst()->deviceSerialNumber));
    tmpResponseData->deviceType = 0x01;

    return commandExcuteResultOk;
}
#endif